package com.sinosoft.brokerchannel.base.utile;

import com.sinosoft.brokerchannel.base.common.ChannelConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cglib.core.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 需求名称：[大童理赔渠道平台]<br/>
 * 类描述:[对象复制处理]<br/>
 *
 * @author shilk
 * @version V1.0
 * @date 2017-12-15 15:23
 */

public class ConverterBeanCopierForCopy implements Converter {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public Object convert(Object arg1, Class arg0, Object context){
        if (arg1 instanceof String &&  arg0 == java.util.Date.class) {
            String p = (String) arg1;
            if (p.trim().length() == 0) {
                return null;
            }
            try {
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                return df.parse(p.trim());
            } catch (Exception e) {
                try {
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                    return df.parse(p.trim());
                } catch (ParseException ex) {
                    logger.error("复制String属性TO时间异常", e);
                    return arg1;
                }
            }
        } else if (arg1 instanceof String && arg0 == java.lang.String.class) {
            /* 输入String ,输出String */
            return arg1;
        }  else if (arg1 instanceof Integer && arg0 == java.lang.String.class) {
	        /* 输入Integer ,输出String */
	        return String.valueOf(arg1);
        }  else if (arg1 instanceof String && arg0 == java.lang.Integer.class) {
	        /* 输入Integer ,输出String */
	        return Integer.parseInt(String.valueOf(arg1));
        }  else if (arg1 instanceof java.util.Date) {
            /* 输入Date ,输出String */
            try {
                return  arg1;
            } catch (Exception e) {
                logger.error("复制时间属性异常", e);
                return null;
            }
        }else{
            if(ToolUtils.isNullOrEmpty(arg1)){
                return null;
            }
            String arg1Name = arg1.getClass().getSimpleName();
            String endsVOFlag = "VO";
            String endsListFlag = "List";
            if(arg1Name.endsWith(endsVOFlag)){
	            Object arg0Obj;
	            try{
		            arg0Obj = arg0.newInstance();
		            ToolUtils.copyProperties(arg0Obj, arg1);
	            } catch (Exception e){
                    logger.error("复制VO属性异常", e);
		            return null;
	            }
	            return arg0Obj;
            }else if(arg1Name.endsWith(endsListFlag)){
	            System.out.println(arg0);
	            System.out.println(arg1);
	            List ll = (ArrayList)arg1;
	            String className = ll.get(0).getClass().getName();
	            System.out.println(className);
	            List list = new ArrayList();
	            for(Object obj : ll){
		            try {
			            Class cls = Class.forName(ChannelConstants.BEANCOPIRE.get(className));
			            Object objNew = cls.newInstance();
			            ToolUtils.copyProperties(objNew, obj);
			            list.add(objNew);
		            } catch (Exception e) {
			            e.printStackTrace();
		            }
		         }
	            System.out.println("=====================");
	            return list;
		    }else{
			    return arg1;
		    }
        }
    }

}
